package com.junki.faceserver.exception;

import cn.hutool.extra.servlet.ServletUtil;
import cn.hutool.json.JSONUtil;
import com.junki.faceserver.vo.JsonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

/**
 * @author junki
 * @date 2021/5/28 10:05
 */
@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    @ExceptionHandler(Exception.class)
    public JsonResult exceptionHandler(HttpServletRequest request, Exception e) {

        log.error("远程地址：{}；请求地址：{}；请求参数：{}；异常信息：{}；", ServletUtil.getClientIP(request), request.getRequestURL(), JSONUtil.toJsonStr(request.getParameterMap()), e.toString(), e);

        return JsonResult.builder().code(400).msg("请求错误").data(e.toString()).build();
    }

}
